<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/18 0018
 * Time: 下午 6:21
 */
namespace app\article\model;
use think\console\command\make\Model;
use think\Db;

class Comment extends Model{
    #评论列表
    public function cm_list($data){
        #条件
        $where = 'a.del=1';
        $where.= isset($data['keyword'])?' and CONCAT(a.content,a.from_userid) like "%'.$data['keyword'].'%"':'';

        $f = 'b.title,a.id,a.content,a.createtime,a.compose_id,c.username';

        #分页
        $total = Db::name('comment')
            ->alias('a')
            ->join('compose b','a.compose_id=b.id')
            ->join('member c','a.from_userid=c.id')
            ->where($where)->field($f)->count();
        $page = ceil($total/$data['limits']);
        if ($data['curr']>$page){
            $data['curr']=$page;
        }

        $list = Db::name('comment')
            ->alias('a')
            ->join('compose b','a.compose_id=b.id')
            ->join('member c','a.from_userid=c.id')
            ->field($f)
            ->where($where)->page($data['curr'],$data['limits'])
            ->order(['b.createtime'=>'desc','a.createtime'=>'desc'])->select();
        $res =[
            'list'=>$list,
            'total'=>$total,
            'pages'=>$page,
            'curr'=>$data['curr'],
            'limits'=>$data['limits']
        ];
        return $res;
    }
    #发表评论
    public function cm_add($data){
        $pid = Db::name('compose')->where(['id'=>$data['compose_id']])->field('pid')->find();
        $data['compose_type']=$pid['pid'];
        $add = Db::name('comment')->insertGetId($data);
        return $add;
    }
    #删除评论
    public function cm_del($data){
        $id =$data['id'];
        unset($data['id']);
        $del = Db::name('comment')
            ->where(['id'=>['in',$id],'del'=>1])->update($data);
        return $del;
    }
    #我发表的评论
    public function myCm_list($data){
        #条件
        $where = 'a.del=1 and b.del=1 and a.from_userid='.$data['from_userid'];
        $where.= isset($data['keyword'])?' and CONCAT(a.content,a.from_userid) like "%'.$data['keyword'].'%"':'';

        $f = 'b.title,a.id,a.content,a.createtime,a.compose_id,c.username';

        #分页
        $total = Db::name('comment')
            ->alias('a')
            ->join('compose b','a.compose_id=b.id')
            ->join('member c','b.userid=c.id')
            ->where($where)->field($f)->count();
        $page = ceil($total/$data['limits']);
        if ($data['curr']>$page){
            $data['curr']=$page;
        }

        $list = Db::name('comment')
            ->alias('a')
            ->join('compose b','a.compose_id=b.id')
            ->join('member c','b.userid=c.id')
            ->field($f)
            ->where($where)->page($data['curr'],$data['limits'])
            ->order(['b.createtime'=>'desc','a.createtime'=>'desc'])->select();
        $res =[
            'list'=>$list,
            'total'=>$total,
            'pages'=>$page,
            'curr'=>$data['curr'],
            'limits'=>$data['limits']
        ];
        return $res;
    }
    #我的文章评论
    public function otherCm_list($data){
        #条件
        $where = 'a.del=1 and b.del=1 and a.userid='.$data['from_userid'];
        $where.= isset($data['keyword'])?' and CONCAT(a.content,a.from_userid) like "%'.$data['keyword'].'%"':'';

        $f = 'a.title,a.id as compose_id,b.content,b.createtime,b.id,c.username';

        #分页
        $total = Db::name('compose')
            ->alias('a')
            ->join('comment b','a.id=b.compose_id')
            ->join('member c','b.from_userid=c.id')
            ->where($where)->field($f)->count();
        $page = ceil($total/$data['limits']);
        if ($data['curr']>$page){
            $data['curr']=$page;
        }

        $list = Db::name('compose')
            ->alias('a')
            ->join('comment b','a.id=b.compose_id')
            ->join('member c','b.from_userid=c.id')
            ->field($f)
            ->where($where)->page($data['curr'],$data['limits'])
            ->order(['a.createtime'=>'desc','b.createtime'=>'desc'])->select();
        foreach ($list as $k =>$v){
            $list[$k]['reply']=Db::name('reply')
                ->alias('a')
                ->join('member b','b.id=a.from_userid')
                ->join('member c','c.id=a.to_userid')
                ->field('a.id,a.content,a.createtime,b.username as from_name,c.username as to_name')
                ->where(['a.comment_id'=>$v['id'],'a.del'=>1])->select();
        }
        $res =[
            'list'=>$list,
            'total'=>$total,
            'pages'=>$page,
            'curr'=>$data['curr'],
            'limits'=>$data['limits']
        ];
        return $res;
    }
}